package com.flyme.module.order.provider.service;

import com.egzosn.pay.common.api.PayConfigStorage;
import com.egzosn.pay.common.api.PayMessageRouter;
import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.egzosn.pay.common.http.UriVariables;
import com.flyme.module.order.client.entity.Order;
import com.flyme.module.order.client.entity.PayAccount;
import com.flyme.module.order.client.entity.PayBeforeHandler;
import com.flyme.module.order.client.enums.OrderTypeEnum;
import com.flyme.module.order.client.entity.MyPayOrder;
import com.flyme.module.order.client.vo.PayType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Map;

/**
 * 支付响应对象
 */
public class PayResponse {

    @Resource
    private AutowireCapableBeanFactory spring;

    private PayConfigStorage storage;

    private PayService<?> service;

    private Object payInfo;

    private PayMessageRouter router;
    private Map<String, Object> map;
    @Autowired
    private OrderService orderService;

    public PayResponse() {

    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    /**
     * 初始化支付配置
     */
    public void init(HttpServletRequest request, PayAccount payAccount) {
        // 根据不同的账户类型 初始化支付配置
        String orderId = request.getParameter("orderId");
        Order order = orderService.getById(orderId);
        payAccount.setOrder(order);
        String ot = order.getOrderEntity().code;
        String handlerName = OrderTypeEnum.getName(ot);
        this.service = payAccount.getPayType().getPayService(payAccount);
        this.storage = service.getPayConfigStorage();
        // 这里设置http请求配置
        // feign.setRequestTemplateConfigStorage(getHttpConfigStorage());
        PayBeforeHandler payBeforeHandler = spring.getBean(handlerName + "PayHandler", PayBeforeHandler.class);
        MyPayOrder myPayOrder = payBeforeHandler.getPayOrder(request);
        Boolean test = myPayOrder.getTest();
        myPayOrder.setSubject(order.getOrderTitle());
        myPayOrder.setBody(order.getOrderBody());
        try {
            myPayOrder.setAddition(URLEncoder.encode(handlerName, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        myPayOrder.setPrice(test ? new BigDecimal("0.05") : order.getOrderAmount());
        myPayOrder.setOutTradeNo(order.getOutTradeNo());
        myPayOrder.setTransactionType(payAccount.getPayType().getTransactionType(myPayOrder.getMethedType()));
        if (payAccount.getPayType().equals(PayType.aliPay)) {
            setPayInfo(UriVariables.getMapToParameters(service.orderInfo(myPayOrder)));
        } else {
            setPayInfo(service.orderInfo(myPayOrder));
        }
    }

    public void initPC(HttpServletRequest request, PayAccount payAccount) {
        String orderId = request.getParameter("orderId");
        Order order = orderService.getById(orderId);
        payAccount.setOrder(order);
        // 根据不同的账户类型 初始化支付配置
        this.service = payAccount.getPayType().getPayService(payAccount);
        this.storage = service.getPayConfigStorage();
    }

    /**
     * 初始化回调配置
     */
    public void setCallBack(HttpServletRequest request, PayAccount payAccount) {
        // 根据不同的账户类型 初始化支付配置
        this.service = payAccount.getPayType().getPayService(payAccount);
        this.storage = service.getPayConfigStorage();
        // 这里设置http请求配置
        // feign.setRequestTemplateConfigStorage(getHttpConfigStorage());
        String name = payAccount.getOrder().getOrderEntity().code;
        String handlerName = OrderTypeEnum.getName(name);
        PayBeforeHandler payBeforeHandler = spring.getBean(handlerName + "PayHandler", PayBeforeHandler.class);
        service.setPayMessageHandler(payBeforeHandler);
        service.addPayMessageInterceptor(payBeforeHandler);
    }

    /**
     * 获取http配置，如果配置为null则为默认配置，无代理,无证书的请求方式。 此处非必需
     */
    public HttpConfigStorage getHttpConfigStorage(PayAccount payAccount) {
        HttpConfigStorage httpConfigStorage = new HttpConfigStorage();
        /* 网路代理配置 根据需求进行设置 */
        // //http代理地址
        // httpConfigStorage.setHttpProxyHost("192.168.1.69");
        // //代理端口
        // httpConfigStorage.setHttpProxyPort(3308);
        // //代理用户名
        // httpConfigStorage.setHttpProxyUsername("user");
        // //代理密码
        // httpConfigStorage.setHttpProxyPassword("password");
        // 设置ssl证书路径 https证书设置 方式二
        httpConfigStorage.setKeystorePath(payAccount.getKeystorePath());
        // 设置ssl证书对应的密码
        httpConfigStorage.setStorePassword(payAccount.getStorePassword());
        return httpConfigStorage;
    }

    public PayConfigStorage getStorage() {
        return storage;
    }

    public PayService<?> getService() {
        return service;
    }

    public Object getPayInfo() {
        return payInfo;
    }

    public void setPayInfo(Object payInfo) {
        this.payInfo = payInfo;
    }

    public PayMessageRouter getRouter() {
        return router;
    }
}
